<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/yuwanzi.io/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/yuwanzi.io/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/yuwanzi.io/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/yuwanzi.io/images/logo.svg" color="#222">

<link rel="stylesheet" href="/yuwanzi.io/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"suyuhuan.gitee.io","root":"/yuwanzi.io/","images":"/yuwanzi.io/images","scheme":"Muse","version":"8.2.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Suche...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="概述&amp;nbsp;&amp;nbsp;Spring Data是Spring用来解决数据访问的解决方案,它包含了大量关系型数据库以及NoSQL数据库的数据持久层访问解决方案.">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring数据访问方案-Spring Data">
<meta property="og:url" content="https://suyuhuan.gitee.io/yuwanzi.io/2016/07/31/2016-07-31-Spring-data/index.html">
<meta property="og:site_name" content="玉丸子 | Blog">
<meta property="og:description" content="概述&amp;nbsp;&amp;nbsp;Spring Data是Spring用来解决数据访问的解决方案,它包含了大量关系型数据库以及NoSQL数据库的数据持久层访问解决方案.">
<meta property="og:locale">
<meta property="og:image" content="http://ww2.sinaimg.cn/mw690/63503acbjw1f6j3zxk8f5j20i104imxu.jpg">
<meta property="og:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f6j45n3hnxj20q00hs0vq.jpg">
<meta property="og:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f6j45ne4tnj20ti07aabj.jpg">
<meta property="og:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f6j94ghv86j20ma0bjtaw.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/mw690/63503acbjw1f6j9asrv0sj20dq0543yx.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/mw690/63503acbjw1f6jatc82fnj20rf06375q.jpg">
<meta property="og:image" content="http://ww2.sinaimg.cn/mw690/63503acbjw1f6jatcdurzj20kq02uaar.jpg">
<meta property="og:image" content="http://ww2.sinaimg.cn/mw690/63503acbjw1f6jatcnebfj20jn05k0tl.jpg">
<meta property="og:image" content="http://ww4.sinaimg.cn/mw690/63503acbjw1f6jthq9ojpj20do05fgml.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/mw690/63503acbjw1f6judons88j20pz0cugoi.jpg">
<meta property="og:image" content="http://ww2.sinaimg.cn/mw690/63503acbjw1f6judoyj61j20go031q3d.jpg">
<meta property="og:image" content="http://ww4.sinaimg.cn/mw690/63503acbjw1f6julp4p8mj20ph0c40v9.jpg">
<meta property="og:image" content="http://ww2.sinaimg.cn/mw690/63503acbjw1f6jv8nwlvej20j006n0tw.jpg">
<meta property="og:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f6jv8o9wlrj20qp0dyn0m.jpg">
<meta property="og:image" content="http://ww2.sinaimg.cn/mw690/63503acbjw1f6jv8oxqiwj20t00nxgsx.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/mw690/63503acbjw1f6l1pub41ej20hw01ydga.jpg">
<meta property="og:image" content="http://ww4.sinaimg.cn/mw690/63503acbjw1f6l41qx7pbj20qa0am775.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/mw690/63503acbjw1f6l51jk3k8j20m707iwgh.jpg">
<meta property="og:image" content="http://ww2.sinaimg.cn/mw690/63503acbjw1f6l5oypp5fj20j90fxwhx.jpg">
<meta property="article:published_time" content="2016-07-31T10:00:00.000Z">
<meta property="article:modified_time" content="2020-11-07T00:58:17.000Z">
<meta property="article:author" content="玉丸子">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Spring">
<meta property="article:tag" content="Spring Boot">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://ww2.sinaimg.cn/mw690/63503acbjw1f6j3zxk8f5j20i104imxu.jpg">


<link rel="canonical" href="https://suyuhuan.gitee.io/yuwanzi.io/2016/07/31/2016-07-31-Spring-data/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh'
  };
</script>
<title>Spring数据访问方案-Spring Data | 玉丸子 | Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/yuwanzi.io/atom.xml" title="玉丸子 | Blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Navigationsleiste an/ausschalten" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/yuwanzi.io/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">玉丸子 | Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Inhaltsverzeichnis
        </li>
        <li class="sidebar-nav-overview">
          Übersicht
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A6%82%E8%BF%B0"><span class="nav-number">1.</span> <span class="nav-text">概述</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Spring-Data-Commons"><span class="nav-number">1.1.</span> <span class="nav-text">Spring Data Commons</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Spring-Data-Repository"><span class="nav-number">1.2.</span> <span class="nav-text">Spring Data Repository</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Spring-Data-JPA"><span class="nav-number">2.</span> <span class="nav-text">Spring Data JPA</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#JPA%E8%A7%84%E8%8C%83"><span class="nav-number">2.1.</span> <span class="nav-text">JPA规范</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E8%AE%BF%E9%97%AE%E5%B1%82"><span class="nav-number">2.2.</span> <span class="nav-text">数据访问层</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9F%A5%E8%AF%A2%E6%96%B9%E6%B3%95"><span class="nav-number">2.3.</span> <span class="nav-text">查询方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9F%A5%E8%AF%A2%E5%85%B3%E9%94%AE%E5%AD%97"><span class="nav-number">2.4.</span> <span class="nav-text">查询关键字</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Query-amp-NamedQuery%E6%9F%A5%E8%AF%A2"><span class="nav-number">2.5.</span> <span class="nav-text">@Query&amp;@NamedQuery查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9B%B4%E6%96%B0%E6%9F%A5%E8%AF%A2"><span class="nav-number">2.6.</span> <span class="nav-text">更新查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Specification"><span class="nav-number">2.7.</span> <span class="nav-text">Specification</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%8E%92%E5%BA%8F-amp-%E5%88%86%E9%A1%B5"><span class="nav-number">2.8.</span> <span class="nav-text">排序&amp;分页</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89Repository"><span class="nav-number">2.9.</span> <span class="nav-text">自定义Repository</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Spring-Boot%E6%94%AF%E6%8C%81"><span class="nav-number">2.10.</span> <span class="nav-text">Spring Boot支持</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Spring-Data-REST"><span class="nav-number">3.</span> <span class="nav-text">Spring Data REST</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Spring-Boot%E6%94%AF%E6%8C%81-1"><span class="nav-number">3.1.</span> <span class="nav-text">Spring Boot支持</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Spring-Data-MongoDB"><span class="nav-number">4.</span> <span class="nav-text">Spring Data MongoDB</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Spring-Boot%E6%94%AF%E6%8C%81-2"><span class="nav-number">4.1.</span> <span class="nav-text">Spring Boot支持</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Spring-Data-Redis"><span class="nav-number">5.</span> <span class="nav-text">Spring Data Redis</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Serializer"><span class="nav-number">5.1.</span> <span class="nav-text">Serializer</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Spring-Boot%E6%94%AF%E6%8C%81-3"><span class="nav-number">5.2.</span> <span class="nav-text">Spring Boot支持</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#end"><span class="nav-number">6.</span> <span class="nav-text">end</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">玉丸子</p>
  <div class="site-description" itemprop="description">这里是玉丸子的个人博客,与你一起发现更大的世界。</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/yuwanzi.io/archives">
          <span class="site-state-item-count">68</span>
          <span class="site-state-item-name">Artikel</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/yuwanzi.io/categories/">
        <span class="site-state-item-count">39</span>
        <span class="site-state-item-name">Kategorien</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/yuwanzi.io/tags/">
        <span class="site-state-item-count">46</span>
        <span class="site-state-item-name">schlagwörter</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh">
    <link itemprop="mainEntityOfPage" href="https://suyuhuan.gitee.io/yuwanzi.io/2016/07/31/2016-07-31-Spring-data/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/yuwanzi.io/images/avatar.gif">
      <meta itemprop="name" content="玉丸子">
      <meta itemprop="description" content="这里是玉丸子的个人博客,与你一起发现更大的世界。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="玉丸子 | Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Spring数据访问方案-Spring Data
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Veröffentlicht am</span>

      <time title="Erstellt: 2016-07-31 18:00:00" itemprop="dateCreated datePublished" datetime="2016-07-31T18:00:00+08:00">2016-07-31</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Bearbeitet am</span>
        <time title="Geändert am: 2020-11-07 08:58:17" itemprop="dateModified" datetime="2020-11-07T08:58:17+08:00">2020-11-07</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">in</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">后端</span></a>
        </span>
          . 
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
          . 
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/Java/Spring-Boot/" itemprop="url" rel="index"><span itemprop="name">Spring Boot</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h3 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h3><p>&nbsp;&nbsp;Spring Data是Spring用来解决数据访问的解决方案,它包含了大量关系型数据库以及NoSQL数据库的数据持久层访问解决方案.<br>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |</p>
<h4 id="Spring-Data-Commons"><a href="#Spring-Data-Commons" class="headerlink" title="Spring Data Commons"></a>Spring Data Commons</h4><p>&nbsp;&nbsp;Spring Data提供了统一的API来对各种数据存储技术进行数据访问操作,这是通过Spring Data Commons来实现的,它是所有Spring Data子项目的依赖.</p>
<h4 id="Spring-Data-Repository"><a href="#Spring-Data-Repository" class="headerlink" title="Spring Data Repository"></a>Spring Data Repository</h4><p>&nbsp;&nbsp;Spring Data Repository是数据访问的统一标准,它是抽象的,不同的数据访问技术有不同的Repository,它的顶级接口为Repository接口.</p>
<p><img src="http://ww2.sinaimg.cn/mw690/63503acbjw1f6j3zxk8f5j20i104imxu.jpg"></p>
<p><strong>&nbsp;&nbsp;Repository用一个实体类型与ID类型作为泛型.</strong></p>
<p>&nbsp;&nbsp;Repository的子接口CrudRepository定义了CRUD操作的相关内容:</p>
<p><img src="http://ww3.sinaimg.cn/mw690/63503acbjw1f6j45n3hnxj20q00hs0vq.jpg"></p>
<p>&nbsp;&nbsp;CrudRepository的子接口PagingAndSortingRepository定义了分页与排序的相关内容:</p>
<p><img src="http://ww3.sinaimg.cn/mw690/63503acbjw1f6j45ne4tnj20ti07aabj.jpg"></p>
<h3 id="Spring-Data-JPA"><a href="#Spring-Data-JPA" class="headerlink" title="Spring Data JPA"></a>Spring Data JPA</h3><h4 id="JPA规范"><a href="#JPA规范" class="headerlink" title="JPA规范"></a>JPA规范</h4><hr>
<p>&nbsp;&nbsp;JPA是Java Persistence API的缩写,它是一个基于O/R(Object-Relational Mapping)映射的标准规范.例如Hibernate就是JPA规范的实现.</p>
<h4 id="数据访问层"><a href="#数据访问层" class="headerlink" title="数据访问层"></a>数据访问层</h4><hr>
<ol>
<li>在Spring Data JPA中定义数据访问层首先需要继承JpaRepository接口.</li>
<li>之后可以通过@EnableJpaRepository注解开启Spring Data JPA的支持,@EnableJpaRepository接收的value参数用于扫描数据访问层所在包下的接口定义.</li>
</ol>
<h4 id="查询方法"><a href="#查询方法" class="headerlink" title="查询方法"></a>查询方法</h4><hr>
<p>&nbsp;&nbsp;Spring Data JPA支持通过定义在Repository接口中的方法名来定义查询,方法名是根据实体类的属性名来确定的.</p>
<p><img src="http://ww3.sinaimg.cn/mw690/63503acbjw1f6j94ghv86j20ma0bjtaw.jpg"></p>
<p>&nbsp;&nbsp;其中findBy关键字可以用find、read、readBy、query、queryBy、get、getBy替代.</p>
<p>&nbsp;&nbsp;Spring Data JPA可以使用top和first关键字查询指定数量的数据.</p>
<p><img src="http://ww1.sinaimg.cn/mw690/63503acbjw1f6j9asrv0sj20dq0543yx.jpg"></p>
<h4 id="查询关键字"><a href="#查询关键字" class="headerlink" title="查询关键字"></a>查询关键字</h4><hr>
<table>
<thead>
<tr>
<th>关键字</th>
<th>示例</th>
<th>同功能JPQL</th>
</tr>
</thead>
<tbody><tr>
<td>And</td>
<td>findByNameAndAge</td>
<td>where x.name = ?1 and x.age = ?2</td>
</tr>
<tr>
<td>Or</td>
<td>findByNameOrAge</td>
<td>where x.name = ?1 or x.age = ?2</td>
</tr>
<tr>
<td>Is</td>
<td>findByNameIs</td>
<td>where x.name = ?1</td>
</tr>
<tr>
<td>Equals</td>
<td>findByNameEquals</td>
<td>where x.name = ?1</td>
</tr>
<tr>
<td>Between</td>
<td>findByAgeBetween</td>
<td>where x.age between ?1 and ?2</td>
</tr>
<tr>
<td>LessThan</td>
<td>findByAgeLessThan</td>
<td>where x.age &lt; ?1</td>
</tr>
<tr>
<td>LessThanEqual</td>
<td>findByAgeLessThanEqual</td>
<td>where x.age &lt;= ?1</td>
</tr>
<tr>
<td>GreaterThan</td>
<td>findByAgeGreaterThan</td>
<td>where x.age &gt; ?1</td>
</tr>
<tr>
<td>GreaterThanEqual</td>
<td>findByAgeGreaterThanEqual</td>
<td>where x.age &gt;= ?1</td>
</tr>
<tr>
<td>After</td>
<td>findByStartDateAfter</td>
<td>where x.startDate &gt; ?1</td>
</tr>
<tr>
<td>Before</td>
<td>findByStartDateBefore</td>
<td>where x.startDate &lt; ?1</td>
</tr>
<tr>
<td>IsNull</td>
<td>findByNameIsNull</td>
<td>where x.name is null</td>
</tr>
<tr>
<td>IsNotNull&amp;NotNull</td>
<td>findByName(Is)NotNull</td>
<td>where x.name not null</td>
</tr>
<tr>
<td>Like</td>
<td>findByNameLike</td>
<td>where x.name like ?1</td>
</tr>
<tr>
<td>NotLike</td>
<td>findByNameNotLike</td>
<td>where x.name not like ?1</td>
</tr>
<tr>
<td>StartingWith</td>
<td>findByNameStartingWith</td>
<td>where x.name like ?1(参数前面加%)</td>
</tr>
<tr>
<td>EndingWith</td>
<td>findByNameEndingWith</td>
<td>where x.name like ?1(参数后面加%)</td>
</tr>
<tr>
<td>Containing</td>
<td>findByNameContaining</td>
<td>where x.name like ?1(参数前后都加%)</td>
</tr>
<tr>
<td>OrderBy</td>
<td>findByNameOrderByAgeDesc</td>
<td>where x.name = ?1 order by x.age desc</td>
</tr>
<tr>
<td>Not</td>
<td>findByNameNot</td>
<td>where x.name &lt;&gt; ?1</td>
</tr>
<tr>
<td>In</td>
<td>findByAgeIn(Collection<Age> age)</td>
<td>where x.age in ?1</td>
</tr>
<tr>
<td>NotIn</td>
<td>findByAgeNotIn(Collection<Age> age)</td>
<td>where x.age not in ?1</td>
</tr>
<tr>
<td>True</td>
<td>findByActiveTrue()</td>
<td>where x.active = true</td>
</tr>
<tr>
<td>False</td>
<td>findByActiveFalse()</td>
<td>where x.active = false</td>
</tr>
<tr>
<td>IgnoreCase</td>
<td>findByNameIgnoreCase</td>
<td>where UPPER(x.name) = UPPER(?1)</td>
</tr>
</tbody></table>
<h4 id="Query-amp-NamedQuery查询"><a href="#Query-amp-NamedQuery查询" class="headerlink" title="@Query&amp;@NamedQuery查询"></a>@Query&amp;@NamedQuery查询</h4><hr>
<p>&nbsp;&nbsp;Spring Data JPA支持使用@Query注解在接口的方法上实现查询.</p>
<p><img src="http://ww1.sinaimg.cn/mw690/63503acbjw1f6jatc82fnj20rf06375q.jpg"></p>
<p>&nbsp;&nbsp;Spring Data JPA支持使用@NamedQuery定义查询方法,一个名称映射一条查询语句.</p>
<p><img src="http://ww2.sinaimg.cn/mw690/63503acbjw1f6jatcdurzj20kq02uaar.jpg"></p>
<p><img src="http://ww2.sinaimg.cn/mw690/63503acbjw1f6jatcnebfj20jn05k0tl.jpg"></p>
<h4 id="更新查询"><a href="#更新查询" class="headerlink" title="更新查询"></a>更新查询</h4><hr>
<p>&nbsp;&nbsp;Spring Data JPA支持使用@Modifying注解和@Query注解组合进行数据更新操作.</p>
<p><img src="http://ww4.sinaimg.cn/mw690/63503acbjw1f6jthq9ojpj20do05fgml.jpg"></p>
<h4 id="Specification"><a href="#Specification" class="headerlink" title="Specification"></a>Specification</h4><hr>
<p>&nbsp;&nbsp;Spring Data JPA提供了一个Specification接口可以让我们快速地构造基于准则的查询.通过重写Specification接口的toPredicate方法用来构造查询条件.</p>
<ol>
<li><p>数据访问接口需继承JpaSpecificationExecutor接口.</p>
</li>
<li><p>构造查询条件类</p>
<p><img src="http://ww1.sinaimg.cn/mw690/63503acbjw1f6judons88j20pz0cugoi.jpg"></p>
</li>
<li><p>调用条件查询</p>
<p><img src="http://ww2.sinaimg.cn/mw690/63503acbjw1f6judoyj61j20go031q3d.jpg"></p>
</li>
</ol>
<h4 id="排序-amp-分页"><a href="#排序-amp-分页" class="headerlink" title="排序&amp;分页"></a>排序&amp;分页</h4><hr>
<p>&nbsp;&nbsp;Spring Data JPA提供了Sort类和Page接口及Pageable接口完成排序和分页.</p>
<p><img src="http://ww4.sinaimg.cn/mw690/63503acbjw1f6julp4p8mj20ph0c40v9.jpg"></p>
<h4 id="自定义Repository"><a href="#自定义Repository" class="headerlink" title="自定义Repository"></a>自定义Repository</h4><hr>
<p>&nbsp;&nbsp;如果我们想自定义Repository,可以继承Repository的子接口PagingAndSortingRepository.</p>
<ul>
<li><p>定义自定义的Repository接口.</p>
<p><img src="http://ww2.sinaimg.cn/mw690/63503acbjw1f6jv8nwlvej20j006n0tw.jpg"></p>
</li>
<li><p>定义自定义的Repository接口实现.</p>
<p><img src="http://ww3.sinaimg.cn/mw690/63503acbjw1f6jv8o9wlrj20qp0dyn0m.jpg"></p>
</li>
<li><p>定义RepositoryFactoryBean</p>
<p><img src="http://ww2.sinaimg.cn/mw690/63503acbjw1f6jv8oxqiwj20t00nxgsx.jpg"></p>
</li>
<li><p>开启自定义支持需要使用@EnableJpaRepositories注解的repositoryFactoryBeanClass指定FactoryBean.</p>
</li>
</ul>
<h4 id="Spring-Boot支持"><a href="#Spring-Boot支持" class="headerlink" title="Spring Boot支持"></a>Spring Boot支持</h4><hr>
<ul>
<li><p>Spring Boot使用<code>spring.datasource</code>前缀用来配置dataSource.</p>
</li>
<li><p>Spring Boot自动开启了注解事务支持(@EnableTransactionManagement),并配置了jdbcTemplate.</p>
</li>
<li><p>Spring Boot提供了初始化数据的功能,在类路径下的schema.sql文件会自动初始化表结构;在类路径下的data.sql文件会自动插入表数据.</p>
</li>
<li><p>Spring Boot为我们自动配置了transactionManager、jpaVendorAdapter、entityManagerFactory等Bean.JpaBaseConfiguration还有一个getPackagesToScan方法用于自动扫描带有注解@Entity的实体类</p>
</li>
<li><p>Spring Boot自动配置了OpenEntityManagerInViewInterceptor拦截器,并注册到了Spring MVC的拦截器中.解决了页面访问数据时会话连接已关闭的错误.</p>
</li>
<li><p>Spring Boot自动开启了对Spring Data JPA的支持,无需再配置类中显式声明@EnableJpaRepositories.</p>
</li>
</ul>
<p>&nbsp;&nbsp;在Spring Boot下使用Spring Data JPA,只需要添加依赖spring-boot-stater-data-jpa,然后定义DataSource、实体类、数据访问层即可,无需其他配置.</p>
<h3 id="Spring-Data-REST"><a href="#Spring-Data-REST" class="headerlink" title="Spring Data REST"></a>Spring Data REST</h3><hr>
<p>&nbsp;&nbsp;Spring Data REST支持将Spring Data JPA,Spring Data MongoDB,Spring Data Neo4j,Spring Data GernFile,Spring Data Cassandra的Repository自动转换成REST服务.</p>
<p>&nbsp;&nbsp;Spring Data REST的配置是定义在<strong>RepositoryRestMvcConfiguration</strong>配置类中的,我们可以通过继承这个配置类或者使用@Import注解导入此配置类来使用Spring Data REST.</p>
<h4 id="Spring-Boot支持-1"><a href="#Spring-Boot支持-1" class="headerlink" title="Spring Boot支持"></a>Spring Boot支持</h4><hr>
<p>&nbsp;&nbsp;Spring Boot已经自动配置了<strong>RepositoryRestMvcConfiguration</strong>,所以只需要引入依赖spring-boot-starter-data-rest,不需要任何其他配置.</p>
<ul>
<li><p>Spring Boot使用<code>spring.data.rest</code>前缀用来配置<strong>RepositoryRestMvcConfiguration</strong>的属性.</p>
</li>
<li><p>如果想在自定义的领域类Repository中将方法暴露为REST资源,则需要使用@RestResource注解.<br><img src="http://ww1.sinaimg.cn/mw690/63503acbjw1f6l1pub41ej20hw01ydga.jpg" alt="http://localhost:8080/persons/search/nameStartsWith?name=xx访问此REST资源."></p>
</li>
<li><p>如需要分页,则可以使用参数page=?&amp;size=?来实现分页.<br>例:<a target="_blank" rel="noopener" href="http://localhost:8080/persons/?page=1&amp;size=10">http://localhost:8080/persons/?page=1&amp;size=10</a>.</p>
</li>
<li><p>如需要排序,则可以使用参数sort来实现排序.<br>例:<a target="_blank" rel="noopener" href="http://localhost:8080/persons/?sort=age,desc">http://localhost:8080/persons/?sort=age,desc</a>.</p>
</li>
<li><p>自定义根路径需要在application.properties中设置<code>spring.data.rest.base-path</code>属性.</p>
</li>
<li><p>Spring Data REST的节点路径是默认在实体类之后加s,如果需要自定义节点路径则要在领域类Repository上使用**@RepositoryRestResource**注解的path属性进行设置.</p>
</li>
</ul>
<h3 id="Spring-Data-MongoDB"><a href="#Spring-Data-MongoDB" class="headerlink" title="Spring Data MongoDB"></a>Spring Data MongoDB</h3><hr>
<p>&nbsp;&nbsp;MongoDB是一个基于Document文档的NoSQL数据库,它使用面向对象的思想,每一条记录都是一个文档对象.</p>
<p>&nbsp;&nbsp;Spring Data MongoDB提供了以下的注解用来定义领域类:</p>
<table>
<thead>
<tr>
<th>注解</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>@Document</td>
<td>映射领域对象与MongoDB的一个文档</td>
</tr>
<tr>
<td>@Id</td>
<td>映射当前属性为ID</td>
</tr>
<tr>
<td>@DbRef</td>
<td>当前属性将参考其他文档</td>
</tr>
<tr>
<td>@Field</td>
<td>为文档的属性定义名称</td>
</tr>
<tr>
<td>@Version</td>
<td>将当前属性作为版本</td>
</tr>
</tbody></table>
<p>&nbsp;&nbsp;Spring Data MongoDB还提供了一个MongoTemplate封装了数据访问的方法,我们还需要为MongoClient和MongoDbFactory来配置数据库连接属性.开启MongoDB的Repository需要在配置类上使用注解@EnableMongoRepositories.</p>
<p><img src="http://ww4.sinaimg.cn/mw690/63503acbjw1f6l41qx7pbj20qa0am775.jpg"></p>
<p>&nbsp;&nbsp;定义Spring Data MongoDB的Repository只需要继承MongoRepository接口即可.</p>
<h4 id="Spring-Boot支持-2"><a href="#Spring-Boot支持-2" class="headerlink" title="Spring Boot支持"></a>Spring Boot支持</h4><hr>
<p>&nbsp;&nbsp;使用Spirng Boot主要配置数据库连接、MongoTemplate.可以使用<code>spring.data.mongodb</code>配置MongoDB相关的属性.</p>
<ul>
<li>Spring Boot自动开启了@EnableMongoRepositories注解.</li>
<li>Spring Boot提供了一些默认配置:默认MongoDB端口为27017,服务器为localhost,数据库为test.</li>
<li>在Spring Boot下使用MongoDB只需要引入依赖spring-boot-starter-data-mongodb,不需要其他配置.</li>
</ul>
<h3 id="Spring-Data-Redis"><a href="#Spring-Data-Redis" class="headerlink" title="Spring Data Redis"></a>Spring Data Redis</h3><hr>
<p>&nbsp;&nbsp;Spring Data Redis提供了ConnectionFactory和RedisTemplate.</p>
<ul>
<li><p>根据Redis不同的JavaClient,Spring Data Redis提供了不同的ConnectionFactory.</p>
<ul>
<li>JedisConnectionFactory:使用Jedis作为客户端.</li>
<li>JredisConnectionFactory:使用Jredis作为客户端.</li>
<li>LettuceConnectionFactory:使用Lettuce作为客户端.</li>
<li>SrpConnectionFactory:使用Spullara/redis-protoccol作为客户端.</li>
</ul>
</li>
<li><p>配置ConnectionFactory和RedisTemplate如下:<br><img src="http://ww1.sinaimg.cn/mw690/63503acbjw1f6l51jk3k8j20m707iwgh.jpg"></p>
</li>
</ul>
<p>&nbsp;&nbsp;Spring Data Redis提供了RedisTemplate和StringRedisTemplate两个模板对象进行数据操作.StringRedisTemplate只针对键值都是字符类型的数据进行操作.</p>
<table>
<thead>
<tr>
<th>数据操作方法</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>opsForValue()</td>
<td>操作简单属性的数据</td>
</tr>
<tr>
<td>opsForList()</td>
<td>操作list数据</td>
</tr>
<tr>
<td>opsForSet()</td>
<td>操作set数据</td>
</tr>
<tr>
<td>opsForZSet()</td>
<td>操作ZSet(有序的set)数据</td>
</tr>
<tr>
<td>opsForHash()</td>
<td>操作hash散列的数据</td>
</tr>
</tbody></table>
<h4 id="Serializer"><a href="#Serializer" class="headerlink" title="Serializer"></a>Serializer</h4><hr>
<p>&nbsp;&nbsp;当我们进行存储操作的时候,键值对都是通过Spring提供的Serializer序列化到数据库的.</p>
<ul>
<li>RedisTemplate默认使用的是JdkSerializationRedisSerizlizer.</li>
<li>StringRedisTemplate默认使用的是StringRedisSerializer.</li>
</ul>
<h4 id="Spring-Boot支持-3"><a href="#Spring-Boot支持-3" class="headerlink" title="Spring Boot支持"></a>Spring Boot支持</h4><hr>
<p>&nbsp;&nbsp;Spring Boot默认配置了JedisConnectionFactory、RedisTemplate和StringRedisTemplate.</p>
<p>&nbsp;&nbsp;Spring Boot使用<code>spring.redis</code>为前缀在application.properties中配置Redis相关的属性.</p>
<p><img src="http://ww2.sinaimg.cn/mw690/63503acbjw1f6l5oypp5fj20j90fxwhx.jpg"></p>
<h3 id="end"><a href="#end" class="headerlink" title="end"></a>end</h3><blockquote>
<p>资料参考于 JavaEE开发的颠覆者: Spring Boot实战</p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/yuwanzi.io/tags/Java/" rel="tag"># Java</a>
              <a href="/yuwanzi.io/tags/Spring/" rel="tag"># Spring</a>
              <a href="/yuwanzi.io/tags/Spring-Boot/" rel="tag"># Spring Boot</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2016/07/29/2016-07-29-Spring-boot-autoconfigure/" rel="prev" title="浅谈Spring Boot自动配置的运作原理">
                  <i class="fa fa-chevron-left"></i> 浅谈Spring Boot自动配置的运作原理
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2016/08/01/2016-08-1-Spring-data-transaction/" rel="next" title="Spring Data 事务&缓存">
                  Spring Data 事务&缓存 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">玉丸子</span>
</div>
  <div class="powered-by">Erstellt mit  <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/yuwanzi.io/js/utils.js"></script><script src="/yuwanzi.io/js/motion.js"></script><script src="/yuwanzi.io/js/schemes/muse.js"></script><script src="/yuwanzi.io/js/next-boot.js"></script>

  






  





</body>
</html>
